<?php

class Add extends TPage
{
	private $element_manager;
	private $place_manager;
	private $address_manager;
	
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->element_manager = $this->Application->Modules['conference_elements'];
		$this->place_manager = $this->Application->Modules['places'];
		$this->address_manager = $this->Application->Modules['addresses'];
	}
	
	public function saveButtonClicked($sender, $params)
	{
		if($this->Page->IsValid)
		{
			$place_id = null;
			$address_id = null;
			
			if($this->place_checked->Checked)
			{
				if($this->address_checked->Checked)
				{
					$result = $this->address_manager->addAddress(
														String::format($this->address_form->Country->Text),
														String::format($this->address_form->City->Text),
														String::format($this->address_form->PostalCode->Text),
														String::format($this->address_form->Street->Text),
														String::format($this->address_form->Number->Text),
														String::format($this->address_form->description->Text));
					if($result)
						$address_id = $result->getId();
				}
				
				$result = $this->place_manager->addPlace(
													String::format($this->place_name->Text),
													String::format($this->place_description->Text),
													$address_id);
				if($result)
					$place_id = $result->getId();
			}

			$result = $this->element_manager->addConferenceElement(
													String::format($this->name->Text),
													$this->is_session->getChecked(),
													$this->start_datetime->getDateTime(),
													$this->duration->Text,
													$this->price_checked->Checked ? $this->price->Text : null,
													$this->quantity_checked->Checked ? $this->quantity->Text : null,
													$this->description_checked->Checked ? String::format($this->description->Text) : null,
													$this->User->getName(),
													$place_id,
													$this->is_plan->getChecked());
			
			if($result)
			{
				$this->info->Text = '<p>'.Prado::localize('Changes saved').'</p>';
				$this->clearForm();
			}
			else
				$this->info->Text = '<p>'.Prado::localize('Changes not saved').'</p>';
		}
	}
	
	public function clearForm()
	{
		$this->name->Text = '';
		$this->duration->Text = '';
		$this->price->Text = '';
		$this->is_session->Checked = false;
		$this->price_checked->Checked = false;
		$this->quantity->Text = '';
		$this->quantity_checked->Checked = false;
		$this->description->Text = '';
		$this->description_checked->Checked = false;
		$this->is_plan->Checked = false;
		
		$this->place_checked->Checked = false;
		
		if($this->place_name)
			$this->place_name->Text = '';
		if($this->place_description)
			$this->place_description->Text = '';
		
		$this->address_checked->Checked = false;
			
		if($this->address_form)
			$this->address_form->clearForm();
	}
	
	public function addPlaceVisibleButtonClicked()
	{
		$this->place_panel->Visible = $this->place_checked->Checked;
		$this->place_panel->Enabled = $this->place_checked->Checked;
	}
	
	public function addAddressVisibleButtonClicked()
	{
		$this->address_panel->Visible = $this->address_checked->Checked;
		$this->address_panel->Enabled = $this->address_checked->Checked;
	}
	
	public function getCurrencyName()
	{
		$setting_manager = $this->Application->Modules['settings'];
		if($setting = $setting_manager->getSettingByName(SettingManager::$CURRENCY_NAME))
			return $setting->getValue() != '' ? $setting->getValue() : '-';
		return '-';
	}
}

?>